<?php


class Goods extends AdminBase {

	public function index(){
		
		$page=$this->GetId();
		if ($this->post()){
			$goods_name=$this->input->post()['goods_name'];
			$list=$this->db->like('goods_name',$goods_name,'both')->limit(15,$page)->order_by("create_time", "desc")->get('goods')->result_array();
			$count=count($this->db->like('goods_name',$goods_name,'both')->order_by("create_time", "desc")->get('goods')->result_array());
		}else{
			$list=$this->db->limit(15,$page)->order_by("create_time", "desc")->get('goods')->result_array();
			$count=count($this->db->get('goods')->result_array());
		}
		
		foreach ($list as $k=>$v){
			$img=$this->GetGoodsImage($v['goods_id']);
			if (!empty($img)){
				$list[$k]['image']=$this->GetImg($img[0]['image_id']);
			}else{
				$list[$k]['image']='';
			}
		}
		
		$this->load->library('pagination');
		$config['base_url'] = '/goods/index/';
		$config['total_rows'] = $count;
		$config['per_page'] =15;
		
		$this->pagination->initialize($config);
		$data['pages']=$this->pagination->create_links();
		

		$data['list']=$list;
		$this->load->view('goods/index',$data);

	}

	public function add(){
		if ($this->post()){
			$post=$this->input->post()['goods'];
			$sku=$this->input->post()['sku'];
			$post['create_time']=time();
			$post['update_time']=time();
			//开始事务
			$this->db->trans_begin();

			$this->db->insert('goods',$post);
			$goods_id=$this->db->insert_id();
			
			
			foreach ($sku['name'] as $k=>$v){
			$skudata['name']=$v;
			$skudata['price']=$sku['price'][$k];
			$skudata['weight']=$sku['weight'][$k];
			$skudata['goods_id']=$goods_id;
			$this->db->insert('goods_sku',$skudata);
			}
			
			if (isset($this->input->post()['img_id'])){
				$image=$this->input->post()['img_id'];
				foreach ($image as $v){
			    	$data['goods_id']=$goods_id;
			    	$data['image_id']=$v;
					$this->db->insert('goods_image',$data);
				}
			}

			if ($this->db->trans_status() === FALSE){
				$this->db->trans_rollback();
				$this->show(['code'=>0,'msg'=>'添加失败']);
			}else{
				$this->db->trans_commit();
				$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
	        	$this->cache->delete('goods');
				$this->show(['code'=>1,'msg'=>'添加成功']);
			}

		}

		$this->load->view('goods/add');
	}



	public function edit(){
		if ($this->post()){
			$post=$this->input->post()['goods'];
			$sku=$this->input->post()['sku'];
			$post['update_time']=time();
			//开始事务
			$this->db->trans_begin();
			$this->db->update('goods',$post,array('goods_id'=>$post['goods_id']));


			foreach ($sku['name'] as $k=>$v){
				
			$skudata['name']=$v;
			$skudata['price']=$sku['price'][$k];
			$skudata['goods_id']=$post['goods_id'];
			$skudata['weight']=$sku['weight'][$k];
				
			  if (isset($sku['sku_id'][$k])){
			  $this->db->update('goods_sku',$skudata,array('id'=>$sku['sku_id'][$k]));	
			  }else{
			  $this->db->insert('goods_sku',$skudata);
			  }

			}

			if (isset($this->input->post()['img_id'])){
				$image=$this->input->post()['img_id'];
				foreach ($image as $v){
					$data['goods_id']=$post['goods_id'];
					$data['image_id']=$v;
					$this->db->insert('goods_image',$data);
				}
			}

			if ($this->db->trans_status() === FALSE){
				$this->db->trans_rollback();
				$this->show(['code'=>0,'msg'=>'更新失败']);
			}else{
				$this->db->trans_commit();
				$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
	        	$this->cache->delete('goods');
				$this->show(['code'=>1,'msg'=>'更新成功']);
			}
		}
		$id=$this->GetId();
		$data['list']=$this->db->where('goods_id',$id)->get('goods')->row_array();
		$data['img']=$this->db->where('goods_id',$id)->get('goods_image')->result_array();
        $data['sku']=$this->db->where('goods_id',$id)->get('goods_sku')->result_array();
		foreach ($data['img'] as $k=>$v){
			$data['img'][$k]['img']=$this->GetImg($v['image_id']);
		}
		$this->load->view('goods/edit',$data);
	}

	public function upload(){
		$data=$this->uploadfile();
		if ($data['code']==1){
			$file['url']=$data['url'].$data['upload_data']['file_ext'];
			$file['ext']=$data['upload_data']['file_ext'];
			$this->db->insert('file', $file);
			$id=$this->db->insert_id();
			$this->show(['code'=>1,'url'=>'/'.$file['url'],'img_id'=>$id]);
		}else{
			$this->show(['code'=>0,'msg'=>'上传失败']);
		}
	}

	public function delimg(){
		$id=$this->PostId();
		//开始事务
		$this->db->trans_begin();
		$list=$this->db->where('id',$id)->get('goods_image')->row_array();
		$info=$this->db->where('id',$list['image_id'])->get('file')->row_array();
		unlink($info['url']);
		$this->db->delete('goods_image', array('id'=>$id));

		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$this->show(['code'=>0,'msg'=>'删除失败']);
		}else{
			$this->db->trans_commit();
			$this->show(['code'=>1,'msg'=>'删除成功']);
		}

	}
	
	//
    public function delsku(){
   		$id=$this->PostId();
		//开始事务
		$this->db->trans_begin();
		$this->db->delete('goods_sku', array('id'=>$id));

		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$this->show(['code'=>0,'msg'=>'删除失败']);
		}else{
			$this->db->trans_commit();
			$this->show(['code'=>1,'msg'=>'删除成功']);
		}
    	
    }


	public function del(){
       $id=$this->PostId();
		$this->db->trans_begin();
		$img=$this->db->where('goods_id',$id)->get('goods_image')->result_array();
        foreach ($img as $v){
			$info=$this->db->where('id',$v['image_id'])->get('file')->row_array();
			unlink($info['url']);
			$this->db->delete('goods_image', array('id'=>$v['id']));
		}
		$this->db->delete('goods', array('goods_id'=>$id));
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			$this->show(['code'=>0,'msg'=>'删除失败']);
		}else{
			$this->db->trans_commit();
			$this->show(['code'=>1,'msg'=>'删除成功']);
		}

	}


}
